﻿using System.Collections.Generic;
using System.Text;
namespace CharpClassGen
{
    public class Member
    {
        public string accesser;
        public string extendModifier;
        public bool isAbstruct => extendModifier.Contains("abstract");
        public string type;
        public string name;
        public List<string> attributes = new List<string>();
        protected StringBuilder sb = new StringBuilder();
        public int retrat = 1;//缩进  \t的数量
        private List<string> describe=new List<string> ();

        /// <summary>
        ///  添加一行注释
        /// </summary>
        /// <param name="desc"></param>
        public void AddDescribeOneLine(string desc) 
        {
            describe.Add(desc);
        }

        public static string ExtendModifierStr(ExtendModifier extendModifier)
        {
            return extendModifier.ToString().ToLower().Replace(",", " ").Replace("none", "");
        }

        public static string CodeBlockRetrat(string item, int retrat)
        {
            string[] arr = item.Split("\n");

            if (arr.Length == 1)
            {
                return arr[0];
            }
            StringBuilder sb = new StringBuilder();
            foreach (var line in arr)
            {
                if (!string.IsNullOrWhiteSpace(line))
                {
                    sb.AppendLine(Helper.Retract(retrat) + line);
                }
            }
            return sb.ToString();
        }
        public Member(Accesser accesser, ExtendModifier extendModifier, string type, string name) : this(accesser.ToString(), ExtendModifierStr(extendModifier), type, name)
        {

        }

        public Member(string accesser, string extendModifier, string type, string name)
        {
            this.accesser = accesser.ToLower();
            this.extendModifier = extendModifier;
            this.type = type;
            this.name = name;

        }

        public void AddAttribute(string attribute)
        {

            this.attributes.Add(attribute);
        }

        public override string ToString()
        {

            sb.Clear();
            //添加特性
            foreach (var item in attributes)
            {
                sb.Append(Helper. Retract(retrat));//缩进
                sb.AppendLine($"[{item}]");
            }
            //注释 或者其他文本
            foreach (var item in describe)
            {
                sb.AppendLine($"{Helper.Retract(retrat)}{item}");
            }
        
            //签名部分
            sb.Append(Helper.Retract(retrat));

            sb.Append($"{accesser.ToString().ToLower()} {extendModifier} {type} {name}");
            return sb.ToString();
        }
    }
}



